package com.igeekhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeekhome.dto.DestDto;
import com.igeekhome.dto.SectionDestDto;
import com.igeekhome.mapper.DSpotDestinationMapper;
import com.igeekhome.mapper.DestDtoMapper;
import com.igeekhome.pojo.DSpotDestination;
import com.igeekhome.pojo.DSpotDestinationExample;
import com.igeekhome.service.IDestService;
import com.igeekhome.util.CommonUtil;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Romeo
 */
@Service("destService")
public class DestServiceImpl implements IDestService {

    @Autowired
    private DestDtoMapper destDtoMapper;

    @Autowired
    private DSpotDestinationMapper dSpotDestinationMapper;

    @Override
    public TableResultBean list(Integer nid, String name, Integer id, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        DSpotDestination dSpotDestination = new DSpotDestination();
        dSpotDestination.setId(id);
        dSpotDestination.setName(name);
        dSpotDestination.setOperatorNetwork(nid);
        dSpotDestination.setStatus(1);
        List<DestDto> destDtos = destDtoMapper.selectByNid(dSpotDestination);
        for (DestDto destDto : destDtos) {
            String address = destDto.getAddress();
            String[] addre = address.split(",");
            if(addre.length == 4){
                destDto.setProvince(addre[0]);
                destDto.setCity(addre[1]);
                destDto.setArea(addre[2]);
                destDto.setAddress(addre[3]);
            }
        }
        PageInfo<DestDto> pageInfo = new PageInfo<>(destDtos);
        if(destDtos != null){
            return TableResultBean.ok(pageInfo.getTotal(),destDtos);
        }
        return TableResultBean.fail("暂无数据");
    }

    @Override
    public ResultBean add(DSpotDestination dSpotDestination, Integer staffId, Integer networkId, String province, String city, String area, String address) {
        // 判断从session作用域中取值是否取到，可能会在取值的时候后台Session失效所以需要先判断是否有值
        if(staffId != null && networkId != null){
            String addressId = CommonUtil.toSplicing(province, city, area, address);
            dSpotDestination.setOperator(staffId);
            dSpotDestination.setOperatorNetwork(networkId);
            dSpotDestination.setStatus(1);
            dSpotDestination.setDetailedAddress(addressId);
            int insert = dSpotDestinationMapper.insertSelective(dSpotDestination);
            if(insert > 0){
                return ResultBean.success();
            }
            return ResultBean.fail("新增失败");
        }
        return ResultBean.fail("核心数据丢失，请按F5刷新页面！");
    }

    @Override
    public ResultBean edit(DSpotDestination dSpotDestination, String province, String city, String area, String address) {
        String addressId = CommonUtil.toSplicing(province, city, area, address);
        dSpotDestination.setDetailedAddress(addressId);
        int update = dSpotDestinationMapper.updateByPrimaryKeySelective(dSpotDestination);
        if(update > 0){
            return ResultBean.success();
        }
        return ResultBean.fail("更新失败");
    }

    @Override
    public ResultBean delete(String idsStr) {
        if(idsStr.contains(",")){
            idsStr = idsStr.substring(0,idsStr.length()-1);
        }
        if(!idsStr.contains(",")){
            // 单个删除
            DSpotDestination dSpotDestination = new DSpotDestination();
            dSpotDestination.setStatus(0);
            DSpotDestinationExample example = new DSpotDestinationExample();
            example.createCriteria().andIdEqualTo(Integer.parseInt(idsStr));
            int delete = dSpotDestinationMapper.updateByExampleSelective(dSpotDestination, example);
            if(delete > 0){
                return ResultBean.success();
            }
        }else{
            // 批量删除
            int delete = dSpotDestinationMapper.deleteByIdStr(idsStr);
            if(delete > 0){
                return ResultBean.success();
            }
        }
        return ResultBean.fail("删除失败");
    }

    @Override
    public List<DSpotDestination> getlist() {
        List<DSpotDestination> dSpotDestinations = dSpotDestinationMapper.selectByExample(null);
        return dSpotDestinations;
    }

    @Override
    public List<SectionDestDto> selectDestInIds(String strId) {
        List<SectionDestDto> sectionDestDtos = dSpotDestinationMapper.selectDestStrId(strId);
        return sectionDestDtos;
    }

    @Override
    public List<DSpotDestination> toShow() {
        return dSpotDestinationMapper.selectByExample(null);
    }
}
